#include "RoleScene.h"
#include "GameGlobal.h"
#include "RoomScene.h"
#include "StringTable.h"

using namespace cocos2d;
bool CRoleScene::init()
{
	if(!CCLayer::init())
	{
		return false;
	}

	setIsTouchEnabled(true);
	return true;
}
void CRoleScene::onEnter()
{
	CCLayer::onEnter();

	m_pTipBox = CCocosTipBox::CreateTipBox(CCocosTipBox::TYPE_BMFONT, "Fonts/fnt_ch.fnt", 0.7f, 128, false, "UI/tipBox.png");
	addChild(m_pTipBox, 100);

	std::string str;

	m_pServer = CServerHelper::GetInstance();
	m_pServer->SetGameDelegate(this);
	m_pServer->SetAccountDelegate(NULL);

	m_pSprBack = CCSprite::spriteWithFile("UI/roleBack.png");
	addChild(m_pSprBack);
	m_pSprBack->setPosition(ccpAdjust(SCREEN_WIDTH*0.5f, SCREEN_HEIGHT*0.5f));

	CCSprite *pSprText = CCSprite::spriteWithFile("UI/roleText.png");
	m_pSprBack->addChild(pSprText);
	pSprText->setPosition(ccpAdjustRes(169, 768-120));
	
	CCocosButton *pBtnOk = CCocosButton::CreateButton(ROLE_BTN_OK, this, CCocosButton::TYPE_REPLACE, false, "UI/btn1Up.png", "UI/btn1Down.png");
	m_pSprBack->addChild(pBtnOk);
	pBtnOk->setPosition(ccpAdjustRes(512, 100));

	
	str = STRING_CONFIRM;
	GBKToUTF8(str);
	pBtnOk->CreateLable("Fonts/fnt_ch.fnt", CCTextAlignmentLeft, 0.75f, str.c_str(), str.c_str());

	CCocosInputBox *pibName = CCocosInputBox::CreateEditBox(ROLE_INPUT_NAME, this, "", 24 * CGameGlobal::GetInstance()->m_resScale, "Arial", false, "UI/inputBox.png", CGameGlobal::AdjustResSize(CCSizeMake(180, 35)));
	m_pSprBack->addChild(pibName, 10 , TAG_INPUT_NAME);
	pibName->setPosition(ccpAdjustRes(512,200));

	CCocosCheckBox *pBox = CCocosCheckBox::CreateCheckBox(ROLE_CHECKBOX_TYPE, this);
	m_pSprBack->addChild(pBox, 1, TAG_CHECKBOX_TYPE);
	pBox->setPosition(ccpAdjustRes(169, 768 - 209));
	CCocosButton *pCurBtn;
	
	pCurBtn = pBox->AddBox(ccpAdjustRes(0, 0), 10, ROLE_BTN_ROLE1, false, "UI/btnRoleTypeUp.png", "UI/btnRoleTypeDown.png");
	str = STRING_ROLE + STRING_NUMS[1];
	GBKToUTF8(str);
	pCurBtn->CreateLable("Fonts/fnt_ch.fnt", CCTextAlignmentLeft, 0.75f, str.c_str(), str.c_str());


	pCurBtn = pBox->AddBox(ccpAdjustRes(0, -70), 10, ROLE_BTN_ROLE2, false, "UI/btnRoleTypeUp.png", "UI/btnRoleTypeDown.png");
	str = STRING_ROLE + STRING_NUMS[2];
	GBKToUTF8(str);
	pCurBtn->CreateLable("Fonts/fnt_ch.fnt", CCTextAlignmentLeft, 0.75f, str.c_str(), str.c_str());

	pCurBtn = pBox->AddBox(ccpAdjustRes(0, -140), 10, ROLE_BTN_ROLE3, false, "UI/btnRoleTypeUp.png", "UI/btnRoleTypeDown.png");
	str = STRING_ROLE + STRING_NUMS[3];
	GBKToUTF8(str);
	pCurBtn->CreateLable("Fonts/fnt_ch.fnt", CCTextAlignmentLeft, 0.75f, str.c_str(), str.c_str());

	pBox->SetCheck(ROLE_BTN_ROLE1);

	CCocosCheckBox *pBoxSex = CCocosCheckBox::CreateCheckBox(ROLE_CHECKBOX_SEX, this);
	m_pSprBack->addChild(pBoxSex, 1, TAG_CHECKBOX_SEX);
	pBoxSex->setPosition(ccpAdjustRes(152, 768-438));

	pCurBtn = pBoxSex->AddBox(ccpAdjustRes(-50, 0), 10, ROLE_BTN_MALE, false, "UI/btnRoleSexUp.png", "UI/btnRoleSexDown.png");
	str = STRING_MALE;
	GBKToUTF8(str);
	pCurBtn->CreateLable("Fonts/fnt_ch.fnt", CCTextAlignmentLeft, 0.75f, str.c_str(), str.c_str());

	pCurBtn = pBoxSex->AddBox(ccpAdjustRes(50, 0), 10, ROLE_BTN_FEMALE, false, "UI/btnRoleSexUp.png", "UI/btnRoleSexDown.png");
	str = STRING_FEMALE;
	GBKToUTF8(str);
	pCurBtn->CreateLable("Fonts/fnt_ch.fnt", CCTextAlignmentLeft, 0.75f, str.c_str(), str.c_str());

	pBoxSex->SetCheck(ROLE_BTN_MALE);

	for(int i=0; i<6; i++)
	{
		char szName[64];
		sprintf(szName, "UI/role%d.png", i+1);
		m_pSprRoles[i] = CCSprite::spriteWithFile(szName);
		m_pSprRoles[i]->setPosition(ccpAdjustRes(500, SCREEN_HEIGHT*1.5f));
		m_pSprBack->addChild(m_pSprRoles[i], 100);
		m_pSprRoles[i]->setAnchorPoint(ccp(0.5, 0));
	}

	m_pSprRoles[0]->setPosition(ccpAdjustRes(500, 260));
	m_typeNum = 1;
	m_sexNum = 0;
	

	m_pTipBox->AddControl(pBtnOk);
	m_pTipBox->AddControl(pibName);
	m_pTipBox->AddControl(pBoxSex);
	m_pTipBox->AddControl(pBox);
	
	schedule(schedule_selector(CRoleScene::Tick));
}

void CRoleScene::onReceiveMsg(int aID, int event, const void *pData)
{
	if(aID == ROLE_BTN_OK)
	{
		CCocosInputBox *pib = (CCocosInputBox *)m_pSprBack->getChildByTag(TAG_INPUT_NAME);

		const char *szName = pib->GetText();

		if(strlen(szName) == 0)
		{
			std::string str;

			str = STRING_INPUTNAME;
			GBKToUTF8(str);
			m_pTipBox->ShowTip(str.c_str(), 1.0f);
			return;
		}

		const std::list<int> *pListType = ((CCocosCheckBox *)m_pSprBack->getChildByTag(TAG_CHECKBOX_TYPE))->GetIDList();
		int idType = (*(pListType->begin()));

		const std::list<int> *pListSex = ((CCocosCheckBox *)m_pSprBack->getChildByTag(TAG_CHECKBOX_SEX))->GetIDList();
		int idSex = (*(pListSex->begin()));

		int typeNum = 1;
		int sexNum = 0;

		if(idType == ROLE_BTN_ROLE1)
		{
			typeNum = 1;
		}
		else if(idType == ROLE_BTN_ROLE2)
		{
			typeNum = 3;
		}
		else if(idType == ROLE_BTN_ROLE3)
		{
			typeNum = 5;
		}

		if(idSex == ROLE_BTN_FEMALE)
		{
			sexNum = 1;
		}

		RoleInfo info;

		info._id = typeNum +sexNum;

		strcpy(info._szName, szName);

		std::string str;

		str = STRING_CREATINGROLE;
		GBKToUTF8(str);
		m_pTipBox->ShowTip(str.c_str(), 0);

		m_pServer->CreateRole(info);
	}

	

	if(aID == ROLE_CHECKBOX_SEX || aID == ROLE_CHECKBOX_TYPE)
	{

		int typeNum = m_typeNum;
		int sexNum = m_sexNum;
		switch(event)
		{
		case ROLE_BTN_ROLE1:
		case ROLE_BTN_ROLE2:
		case ROLE_BTN_ROLE3:
			typeNum = (event -ROLE_BTN_ROLE1) * 2 + 1;
			break;
		case ROLE_BTN_MALE:
		case ROLE_BTN_FEMALE:
			sexNum = event - ROLE_BTN_MALE;
			break;
		}

		if(m_typeNum + m_sexNum != typeNum + sexNum)
		{
			m_pSprRoles[m_typeNum+m_sexNum-1]->setIsVisible(false);
			m_pSprRoles[m_typeNum+m_sexNum-1]->setPosition(ccpAdjustRes(SCREEN_WIDTH*0.5f, SCREEN_HEIGHT*1.5f));

			m_pSprRoles[typeNum+sexNum-1]->runAction(CCMoveTo::actionWithDuration(0.3f, ccpAdjustRes(SCREEN_WIDTH*0.5f, 260)));
			m_pSprRoles[typeNum+sexNum-1]->setIsVisible(true);

			m_typeNum = typeNum;
			m_sexNum = sexNum;
		}
	}
	
}
void CRoleScene::createNameRepeat()
{
	std::string str;

	str = STRING_ROLEEXIST;
	GBKToUTF8(str);
	m_pTipBox->ShowTip(str.c_str(), 1.0f);

	int kk = 1;
	return;
}
void CRoleScene::loginRegionBack(bool bSuccess)
{
	if(bSuccess)
	{
		CRoomScene::Show();
	}
	else
	{
		std::string str;
		str = STRING_LOGINFAIL;
		GBKToUTF8(str);
		m_pTipBox->ShowTip(str.c_str(), 1.0f);
	}
}

void CRoleScene::Tick(cocos2d::ccTime dt)
{
	m_pServer->ReceivePacket();
}